/**
 * Project Name:filmbAdmin1 File Name:FileUploadAction.java Package
 * Name:com.filmb.admin.fileinfo.action Date:2014年10月31日上午11:28:43 Copyright (c)
 * 2014
 * 
 */
/**
 * @projectName:filmbAdmin1
 * @packageName:com.filmb.admin.fileinfo.action
 * @className:FileUploadAction.java
 * 
 * @createTime:2014年10月31日-上午11:28:43
 * 
 * 
 */

package com.yn.xr.front.user.action;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.yn.xr.common.constants.CommonDef;
import com.yn.xr.common.exception.UserLoginException;
import com.yn.xr.common.exception.base.BaseException;
import com.yn.xr.common.util.StringUtils;
import com.yn.xr.front.base.action.BaseAction;
import com.yn.xr.front.base.constants.SystemConstants;
import com.yn.xr.front.user.service.UserRegisterService;
import com.yn.xr.user.mode.UserRegister;

/**
 * 
 * @description:用户Action
 * @fileName:UserAction.java
 * @createTime:2015年1月20日 下午7:29:36
 * @author:肖震
 * @version 1.0.0
 * 
 */
@Scope("prototype")
@Controller("userAction")
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 5935490475016825193L;
	private static final Logger log = LoggerFactory.getLogger(UserAction.class);
	@Resource
    private UserRegisterService userRegisterService;
	
	// 用户数据实体对象
	private UserRegister userRegister;
	
	
	// 登录校验码
	private String dialogCode;
	
	private String accName;
	
	private String pwd;
	
	/**
	 * 
	 * @function:验证用户登录
	 * @return String
	 * @exception
	 * @author:陈权福
	 * @since 1.0.0
	 */
	public String login() {
		if (userRegister != null) {
			if (!StringUtils.isEmpty(userRegister.getAccName()) && !StringUtils.isEmpty(userRegister.getPwd())) {
				try {
					//TODO 登陆验证吗是否存在不能已界面取值判断
					if (!StringUtils.isEmpty(dialogCode)) {
						String codeString = ((String) super.getSession().get("code")).toUpperCase();
						String dialogCodeString = dialogCode.toUpperCase();
						if (!dialogCodeString.equals(codeString)) {
							throw new UserLoginException("验证码不一致！");
						}
					}
					String accName = userRegister.getAccName();
					// 调用service
					userRegister = userRegisterService.login(accName, userRegister.getPwd());
					
					log.info(userRegister.getAccName() + " login success");
					
					savrUserInfoToSession(userRegister);
					
					Object actionName =   request.getSession().getAttribute(SystemConstants.SYSPARM_ACTIONNAME);
					//Object nameSpace =   request.getSession().getAttribute(SystemConstants.SYSPARM_NAMESPACE);
					if (actionName != null && !actionName.toString().isEmpty()) {
						request.getSession().removeAttribute( SystemConstants.SYSPARM_ACTIONNAME );
						request.getSession().removeAttribute( SystemConstants.SYSPARM_NAMESPACE );
						return "beforeAction";
					}
					return "loginSuccess";// 返回个人中心页面
				} catch (BaseException e) {
					log.error("用户登录失败：" + e.getErrorMsg());
					request.setAttribute("errorMsg", e.getErrorMsg());
					if (e.getErrorMsg().indexOf("可以输入") >= SystemConstants.IS_INCLUDE_STR) {// 密码错误
						String allowInputTimes = e.getErrorMsg().substring( e.getErrorMsg().length() - 2, e.getErrorMsg().length() - 1);
						if (Integer.parseInt(allowInputTimes) < SystemConstants.IS_SHOW_LOGIN_CODE) {// 小于三次，验证码显示
							request.setAttribute("showDialogCode", SystemConstants.lOGIN_SHOW_DIALOG_CODE);
						}
					} else if (e.getErrorMsg().indexOf("解锁后登录") >= SystemConstants.IS_INCLUDE_STR || e.getErrorMsg().indexOf("验证码不一致") >= SystemConstants.IS_INCLUDE_STR) {// 锁定，验证码显示
						request.setAttribute("showDialogCode", SystemConstants.lOGIN_SHOW_DIALOG_CODE);
					}
					return "toLogin";// 返回登录页面
				} catch (Exception e) {
					log.error("查询用户信息失败：" + e.getMessage());
					request.setAttribute("errorMsg", "用户登录失败");
					return "toLogin";// 返回登录页面
				}
			}
			request.setAttribute("errorMsg", "惠信号或密码为空");
		}
		return "toLogin";// 返回登录页面
	}

	/**
	 * @Description:  弹框登录
	 * @author wangpinwen
	 * @date 2015年8月25日 下午12:07:42 
	 * @version V1.0.0
	 * @return
	 */
	public String newsLogin() {
		request.setAttribute(SystemConstants.headFlag, CommonDef.FrontTabSort.NEWSCENTER.getValue());
		if (!StringUtils.isEmpty(userRegister.getAccName()) && !StringUtils.isEmpty(userRegister.getPwd())) {
			try {
				if (!StringUtils.isEmpty(dialogCode)) {
					String codeString = ((String) super.getSession() .get("code")).toUpperCase();
					String dialogCodeString = dialogCode.toUpperCase();
					if (!dialogCodeString.equals(codeString)) {
						super.outStr("验证码不一致");
					}
				}
				String accName = userRegister.getAccName();
				String pwd = userRegister.getPwd();
				// 调用service
				userRegister = userRegisterService.login(accName, pwd);
				log.info(userRegister.getAccName() + " login success");
				
				savrUserInfoToSession(userRegister);

				Object actionName = request.getSession().getAttribute( SystemConstants.SYSPARM_ACTIONNAME);
				//Object nameSpace = request.getSession().getAttribute( SystemConstants.SYSPARM_NAMESPACE );
				if (actionName != null && !actionName.toString().isEmpty()) {
					request.getSession().removeAttribute(  SystemConstants.SYSPARM_ACTIONNAME );
					request.getSession().removeAttribute( SystemConstants.SYSPARM_NAMESPACE );
				}
				super.outStr("true");
			} catch (BaseException e) {
				log.error("用户登录失败：" + e.getErrorMsg());
				// request.setAttribute("errorMsg", e.getErrorMsg());
				if (e.getErrorMsg().indexOf("可以输入") >= SystemConstants.IS_INCLUDE_STR) {// 密码错误
					String allowInputTimes = e.getErrorMsg().substring( e.getErrorMsg().length() - 2, e.getErrorMsg().length() - 1);
					if (Integer.parseInt(allowInputTimes) < SystemConstants.IS_SHOW_LOGIN_CODE) {// 小于三次，验证码显示
						request.setAttribute("showDialogCode", SystemConstants.lOGIN_SHOW_DIALOG_CODE);
					}
				} else if (e.getErrorMsg().indexOf("解锁后登录") >= SystemConstants.IS_INCLUDE_STR
						|| e.getErrorMsg().indexOf("验证码不一致") >= SystemConstants.IS_INCLUDE_STR) {// 锁定，验证码显示
					request.setAttribute("showDialogCode", SystemConstants.lOGIN_SHOW_DIALOG_CODE);
				}
				super.outStr(e.getErrorMsg());
			} catch (Exception e) {
				log.error("查询用户信息失败：" + e.getMessage());
				super.outStr("登录失败");
			}
		} else {
			super.outStr("惠信号或密码为空");
		}
		return null;
	}

	/**
	 * @Description: 登陆成功后保持用户信息到session中
	 * @author wangpinwen
	 * @date 2015年8月25日 下午12:14:09 
	 * @version V1.0.0
	 * @param userRegisterObj
	 */
	private void savrUserInfoToSession(UserRegister userRegisterObj){
		// 将登录成功后用户id放到session中
		super.getSession().put(SystemConstants.USERID_SESSION_INFO, userRegisterObj.getUserId());
		// 将登陆成功后的uid放到session种
		super.getSession().put(SystemConstants.USER_SESSION_UID, userRegisterObj.getUid());
		// 将登录成功后用户惠信号放到session中
		super.getSession().put( SystemConstants.USERACCNAME_SESSION_INFO, userRegisterObj.getAccName());
		// 将用户全部信息放入session
		super.getSession().put( SystemConstants.USER_SESSION_INFO, userRegisterObj );
	}

	
	 /**
     * 
     * @function:用户注销
     * @return String
     * @exception
     * @author:陈权福
     * @since 1.0.0
     */    
	public String logout() {
		/* 清除session中的数据 */
		ActionContext context = ActionContext.getContext();
		Map session = context.getSession();
		session.clear();
		// 返回登录页面
		return "logoutSuccess";
	}
    
	public String getAccName() {
		return accName;
	}

	public void setAccName(String accName) {
		this.accName = accName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public UserRegister getUserRegister() {
		return userRegister;
	}

	public void setUserRegister(UserRegister userRegister) {
		this.userRegister = userRegister;
	}
	
}
